from datetime import datetime
from common.database import db
from model.verify import Verify
from utils.to_md5 import to_md5


def verify_code(email, code):
    code = to_md5(code)
    verify = Verify.query.filter_by(email=email).first()
    if verify is None:
        return "未获取验证码"
    elif verify.val_time < datetime.now():
        db.session.delete(verify)
        db.session.commit()
        return "验证码已过期"
    elif verify.code != code:
        return "验证码错误"
    else:
        db.session.delete(verify)
        db.session.commit()
        return None
